/*
   Copyright (c) 2011, 2014 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-04 Marco Riccardi - initial implementation
 *	2014-12-20 Marco Riccardi - resource leak
 *
 */
package org.mizartools.system;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.TreeMap;

public class VocabularyFile {

	private static ArrayList<Symbols> symbolsList = new ArrayList<Symbols>();
	private static TreeMap<String, Symbols> symbolsMap;

	private static TreeMap<String, Symbols> getMap() {
		if (symbolsMap == null) {
			symbolsMap = new TreeMap<String, Symbols>();
	    	for (Symbols symbols : symbolsList){
	    		symbolsMap.put(symbols.getArticleId(), symbols);
	    	}
		}
		return symbolsMap;
	}
	
	public static ArrayList<Symbols> getSymbolsList() {
		return symbolsList;
	}

	public static boolean hasVocabulary(String aid){
		return getMap().containsKey(aid);
	}

	public static Symbols getSymbols(String aid){
		return getMap().get(aid);
	}
	
	static {
		String mizFiles = EnvironFile.getMIZFILES();
		if (mizFiles != null){
			File file = new File(mizFiles, "mml.vct");
			try {
				FileReader fileReader = new FileReader(file);
				try {
					LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
					String inputLine;
					String tmp;
					String articleId;
					Symbols symbols = null;
					SymbolKind symbolKind;
					String symbolToken = null;
					String symbolNegativeToken = null;
					String symbolPrecedence = null;
					int index;
					while ((inputLine = lineNumberReader.readLine()) != null){
						if (inputLine.startsWith("#")){
							articleId = inputLine.substring(1);
							inputLine = lineNumberReader.readLine();
							if (symbols != null){symbolsList.add(symbols);}
							symbols = new Symbols(articleId);
						} else{
							tmp = inputLine.substring(0, 1);
							symbolKind = SymbolKind.valueOf(tmp);
							inputLine = inputLine.substring(1);
							index = inputLine.indexOf(" ");
							if (index>0){
								symbolToken = inputLine.substring(0, index);
								inputLine = inputLine.substring(index+1);
								index = inputLine.indexOf(" ");
								if (index>0) {
									symbolNegativeToken = inputLine.substring(0, index);
									symbolPrecedence = inputLine.substring(index+1);
								} else {
									int precedence = 0;
									try {precedence = Integer.parseInt(inputLine);}
									catch (NumberFormatException e){}
									if (precedence > 0) {
										symbolNegativeToken = "";
										symbolPrecedence = inputLine;
									} else {
										symbolNegativeToken = inputLine;
										symbolPrecedence = "";
									}
								}	
							} else{
								symbolToken = inputLine;
								symbolNegativeToken = "";
								symbolPrecedence = "";
							}
							symbols.addSymbol(symbolKind, symbolToken, symbolNegativeToken, symbolPrecedence, false);
						}
					}
					lineNumberReader.close();
					if (symbols != null){symbolsList.add(symbols);}
				}
				catch (IOException e) {}
				finally{
					try {
						fileReader.close();
						} 
					catch (IOException e) {}
				}
			} catch (FileNotFoundException e) {}
			// "set", "=", "[", "]", "{", "}"  are syntax symbols
			for (Symbols symbols : symbolsList) {
				if (symbols.getArticleId().equals("HIDDEN")) {
					symbols.addSymbol(0, SymbolKind.M, "set", "", "", true);
					symbols.addSymbol(0, SymbolKind.R, "=", "", "", true);
					
					symbols.addSymbol(0, SymbolKind.K, "?", "", "", true);
					symbols.addSymbol(0, SymbolKind.K, "{", "", "", true);
					symbols.addSymbol(0, SymbolKind.K, "[", "", "", true);
					symbols.addSymbol(0, SymbolKind.L, "?", "", "", true);
					symbols.addSymbol(0, SymbolKind.L, "}", "", "", true);
					symbols.addSymbol(0, SymbolKind.L, "]", "", "", true);
				}
			}
			
		} 
	}
	
	private VocabularyFile(){
	}

}
